package admin

import (
	"gin-demo01/models"
	"github.com/gin-gonic/gin"
)

type BankController struct {
	BaseController
}

func (u BankController) Index(c *gin.Context) {

	//开始事务
	tx := models.DB.Begin()

	defer func() {
		// 遇到错误时回滚事务
		if r := recover(); r != nil {
			tx.Rollback()
			u.Error(c)
		}
	}()

	u1 := models.Bank{
		Id: 1,
	}
	tx.Find(&u1)
	u1.Balance = u1.Balance - 100
	if err := tx.Save(&u1).Error; err != nil {
		tx.Rollback()
		u.Error(c)
	}

	u2 := models.Bank{
		Id: 2,
	}
	tx.Find(&u2)
	u2.Balance = u2.Balance + 100
	if err := tx.Save(&u2).Error; err != nil {
		tx.Rollback()
		u.Error(c)
	}
	tx.Commit()

	u.Success(c)
}
